//
//  WeHomePageController.m
//  WeGalaxy
//
//  Created by Zheng on 2017/7/18.
//  Copyright © 2017年 Zheng. All rights reserved.
//

#import "WeHomePageController.h"
#import "CXCycleView.h"
#import "WeHomePageInterfaceCell.h"
#import "WeHomePageRecommendedCell.h"
#import "WeHomePageHotCell.h"
#import "WeHomePageRecommendedFooterView.h"

@interface WeHomePageController () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) CXCycleView *headerView;
@property (nonatomic, strong) WeHomePageRecommendedFooterView *footerView;

@end

@implementation WeHomePageController

#pragma mark - 点击事件
- (void)ctrlClickWithType:(WeHomePageInterfaceControlType)type {
    switch (type) {
        case WeHomePageInterfaceControlTypeMedia:
            kCX_LOG(@"自媒体");
            break;
        case WeHomePageInterfaceControlTypeFilm:
            kCX_LOG(@"影视植入");
            break;
        case WeHomePageInterfaceControlTypeNet:
            kCX_LOG(@"网络广告");
            break;
        case WeHomePageInterfaceControlTypeOutdoor:
            kCX_LOG(@"户外广告");
            break;
    }
}

#pragma mark - scrollview delegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGPoint point = scrollView.contentOffset;
    
    if (point.y < -kHomePageHeaderHeight-20) {
        CGFloat cHeight = -kHomePageHeaderHeight-20 - point.y;
        CGFloat scale = (kHomePageHeaderHeight+cHeight)/kHomePageHeaderHeight;
        _headerView.transform = CGAffineTransformMakeScale(scale, scale);
        _headerView.frame = (CGRect){-cHeight,-kHomePageHeaderHeight-cHeight,kSCREEN_WIDTH,kHomePageHeaderHeight+cHeight};
        _headerView.pageControl.hidden = YES;
    } else {
        _headerView.pageControl.hidden = NO;
    }
    
    
    if (point.y > -140) {
        [self.navigationController setNavigationBarHidden:NO animated:YES];
    } else {
        [self.navigationController setNavigationBarHidden:YES animated:YES];
    }
    
}


#pragma mark - tableview datasource&delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 2) {
        return 10;
    }
    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        WeHomePageInterfaceCell *cell = [tableView dequeueReusableCellWithIdentifier:kWeHomePageInterfaceCell_ID forIndexPath:indexPath];
        __weak typeof(self) weakSelf = self;
        cell.ctrlClickBlock = ^(WeHomePageInterfaceControlType type) {
            [weakSelf ctrlClickWithType:type];
        };
        return cell;
    } else if (indexPath.section == 1) {
        WeHomePageRecommendedCell *cell = [tableView dequeueReusableCellWithIdentifier:kWeHomePageRecommendedCell_ID forIndexPath:indexPath];
        return cell;
    } else {
        WeHomePageHotCell *cell = [tableView dequeueReusableCellWithIdentifier:kWeHomePageHotCell_ID forIndexPath:indexPath];
        
        if (indexPath.row==1||indexPath.row==0) {
            cell.fiIconImgv.hidden = NO;
        } else {
            cell.fiIconImgv.hidden = YES;
        }
        
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 2) {
        return 25.f;
    } else {
        return .0001f;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 0) {
        return 30.f;
    } else if (section == 1) {
        return 130.f;
    } else {
        return .0001;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 165.f;
    } else if (indexPath.section == 1) {
        return 33.f;
    } else {
        return 80.f;
    }
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section != 2) return;
    kCX_LOG(@"点击了section= %ld  row= %ld",indexPath.section, indexPath.row);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section != 2) {
        return nil;
    } else {
        UILabel *header = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, 25)];
        header.text = @"     ——  热门推荐  ——";
        header.font = kCX_FONT(11);
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, 0.75)];
        lineView.backgroundColor = kRGB_COLOR(228, 227, 229, 1);
        [header addSubview:lineView];
        
        return header;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if (section == 1) {
        if (!_footerView) {
            _footerView = [[WeHomePageRecommendedFooterView alloc] initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, 65)];
            [_footerView rowsForFooterView:5];
        }
        
        __weak typeof(self) weakSelf = self;
        _footerView.footerClickBlock = ^(NSInteger row) {
            [weakSelf footerViewDidSelectWithRow:row];
        };
        
        return _footerView;
        
    } else {
        return nil;
    }
}

#pragma mark - 尾视图点击事件
- (void)footerViewDidSelectWithRow:(NSInteger)row {
    kCX_LOG(@"点击了左右滚动视图row == %ld",row);
}

#pragma mark - 子视图
- (UITableView *)myTableView {
    if (!_myTableView) {
        _myTableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
        _myTableView.backgroundColor = [UIColor whiteColor];
        _myTableView.contentInset = UIEdgeInsetsMake(kHomePageHeaderHeight, 0, 0, 0);
        [_myTableView addSubview:self.headerView];
        [_myTableView registerClass:[WeHomePageInterfaceCell class] forCellReuseIdentifier:kWeHomePageInterfaceCell_ID];
        [_myTableView registerClass:[WeHomePageRecommendedCell class] forCellReuseIdentifier:kWeHomePageRecommendedCell_ID];
        [_myTableView registerClass:[WeHomePageHotCell class] forCellReuseIdentifier:kWeHomePageHotCell_ID];
    }
    return _myTableView;
}

- (CXCycleView *)headerView {
    if (!_headerView) {
        _headerView = [[CXCycleView alloc] initWithFrame:CGRectMake(0, -kHomePageHeaderHeight, kSCREEN_WIDTH, kHomePageHeaderHeight)];

        _headerView.adDataArray = [@[@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1498550246964&di=f006ae0c0881c78dc4d10104f9c6211f&imgtype=0&src=http%3A%2F%2Fimg.game333.net%2Fuploads%2Fshouyou%2F2017%2F05%2F26%2F2017052625734222.jpg",
                                    @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1498551591984&di=460028c0b852a161e8ab1160b66cbb76&imgtype=0&src=http%3A%2F%2Fnewsimg.5054399.com%2Fuploads%2Fuserup%2F1702%2F13155JQ347.jpg",
                                    @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1498551591982&di=f068bdc2a914e8fec75309f62c30ac5f&imgtype=0&src=http%3A%2F%2Fimg1.gamersky.com%2Fimage2016%2F12%2F20161209_gxh_289_1%2Fimage029_S.jpg",
                                    @"http://web.52pk.com/uploads/170227/8_171115_1_lit.jpg"] mutableCopy];
        
    }
    return _headerView;
}

#pragma mark - 视图生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    
    
    [self.view addSubview:self.myTableView];
    
}

#pragma mark - 内存警告
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
